﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using FineUI;
using ZHDJ.BranchCenter.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.DataCaching;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;

namespace ZHDJ.BranchCenter.Module.PartyRepresentative
{
    public partial class PartyRepresentativeStudioEdit : PageBase
    {
        #region 属性
        /// <summary>
        /// 是否和个人中心共用
        /// </summary>
        public override bool IsCommonPage
        {
            get
            {
                return true;
            }
        }
        #endregion
        #region ViewPower
        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                //return IsPersonalCenter ? "PartyRepresentativeStudioAdd_p" : "PartyRepresentativeStudioAdd";
                return "PartyRepresentativeStudioAdd";
            }
        }

        #endregion

        #region 页面属性
        public string EditorContent
        {
            get; set;
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
                LoadData();
            }
        }

        private void BindData()
        {
            if (IsSystemAdmin)
            {
                btnClose.OnClientClick = ActiveWindow.GetHideReference();
            }
            else
            {
                btnClose.Hidden = true;
                btnSaveClose.Text = "保存";
                btnSaveClose.Icon = Icon.SystemSave;
            }
        }

        private void LoadData()
        {

            string id = GetQueryIntValue("id");
            if (id != "")
            {
                var current = DB.PartyRepresentativeStudios.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                txtLinkMan.Text = current.LinkMan;
                txtLinkPhone.Text = current.LinkPhone;
                txtStudioName.Text = current.StudioName;
                txtStudioPhone.Text = current.StudioPhone;
                ddlCategory.SelectedValue = current.Category;
                EditorContent = current.Summary;
                cbxEnabled.Checked = current.Status == 4;
                ddlCategory.Enabled = false;
                ppMember.Enabled = false;
                ppBranch.Enabled = false;
                frppBranch.Hidden = true;
                frppMember.Hidden = true;
                if (ddlCategory.SelectedValue == "1")
                {
                    ppBranch.Text = ControlBusiness.GetBranchPickerText(current.ObjectID);
                    ppBranch.Value = current.ObjectID;
                    frppBranch.Hidden = false;
                }
                else
                {
                    ppMember.Text = ControlBusiness.GetMembersPickerText(current.ObjectID);
                    ppMember.Value = current.ObjectID;
                    frppMember.Hidden = false;
                }
                hdFileId.Text = current.ImageID;
                if (!string.IsNullOrEmpty(hdFileId.Text))
                {
                    imgPhoto.ImageUrl = UpFileBusiness.GetFileUrl(hdFileId.Text);
                    imgPhoto.ImageWidth = Unit.Pixel(300);

                    PageContext.RegisterStartupScript("ReDoLayout();");
                }
            }
        }

        public bool CheckExists()
        {
            var id = GetQueryIntValue("id");
            var rs = false;
            var objectid = ddlCategory.SelectedValue == "1"? this.ppBranch.Value: this.ppMember.Value;
            if (DB.PartyRepresentativeStudios.Count(a => a.ObjectID == objectid && a.ID!= id) >0)
            {
                rs = true;
            }
            return rs;
        }
        #endregion

        #region Events

        private void SaveData()
        {
            if (CheckExists())
            {
                var message = ddlCategory.SelectedValue == "1" ? "组织" : "党员";
                Alert.Show("此"+ message + "已开通党代表工作室，请重新选择！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }
            string id = GetQueryIntValue("id");
            string imageUrl = string.Empty;
            PartyRepresentativeStudio editModel;
            if (id != "")
            {
                //更新
                editModel = DB.PartyRepresentativeStudios.Find(id);
                if (editModel == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                editModel.ModifyTime = DateTime.Now;
                editModel.ModifyBy = CurrentUser.LoginID;
            }
            else
            {
                
                editModel = new PartyRepresentativeStudio();
                editModel.ID = StringHelper.NewID;
                editModel.CreateTime = DateTime.Now;
                editModel.ModifyTime = DateTime.Now;
                editModel.CreateBy = CurrentUser.LoginID;
                editModel.ModifyBy = CurrentUser.LoginID;
                editModel.OrderNo = DB.PartyRepresentativeStudios.Count() + 1;
                DB.PartyRepresentativeStudios.Add(editModel);
            }

            editModel.Status = cbxEnabled.Checked ? 4 : 1;
            editModel.Category = ddlCategory.SelectedValue;

            if (!string.IsNullOrEmpty(hdFileId.Text))
            {
                editModel.ImageID = hdFileId.Text;
            }
            editModel.LinkMan = txtLinkMan.Text;
            editModel.LinkPhone = txtLinkPhone.Text;
            editModel.ObjectID = ddlCategory.SelectedValue == "1" ? ppBranch.Value : ppMember.Value;
            editModel.StudioName = txtStudioName.Text;
            editModel.StudioPhone = txtStudioPhone.Text;
            editModel.Summary = this.Request.Form["editorValue"];
            if (DB.SaveChanges()>0 && ddlCategory.SelectedValue == "2")
            {
                RoleHelper.AddRole(ppMember.Value, SystemParameterCaching.Get(SystemParameterKey.PartyRepresentativeAdminRoleID));
            }
        }

        /// <summary>
        /// 保存并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveData();
            if (IsSystemAdmin)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
            }
            
        }


        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID, "党代表工作室图片", SysEnum.UploadFileType.uploadimage);
            if (rs.IsSuccess)
            {
                hdFileId.Text = rs.FileID;
                imgPhoto.ImageUrl = "~/" + rs.FileUrl;
                imgPhoto.ImageWidth = Unit.Pixel(300);

                PageContext.RegisterStartupScript("ReDoLayout();");
            }
            else
            {
                Alert.Show("上传失败！" + rs.Message, String.Empty);
            }
        }



        #endregion

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            frppBranch.Hidden = true;
            frppMember.Hidden = true;
            if (ddlCategory.SelectedValue == "1")
            {
                frppBranch.Hidden = false;
            }
            else
            {
                frppMember.Hidden = false;
            }
        }
    }
}